// JavaScript Document
function reloadPage(win) {
    var location = win.location;
    location.href = location.pathname + location.search;
}
$(function(){
    //关闭窗口
   $('.closeBtn').click(function(){    
            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
             parent.layer.close(index);
        });
    $('.layer_demolist_show').on('click',function(){
        layer.open({
            type: 1,
            title: false,
            closeBtn: 0,
            area: ['550px'],
            skin: 'layui-layer-nobg', //没有背景色
            shadeClose: true,
            content: "<img width='550' src='"+$(this).attr('src')+"'/>"
        });
    });

	//样式为layer_show_window的iframe layer弹出
    $('.layer_show_window').on('click',function(e){
		e.preventDefault();
        var url = $(this).data("url");
        var width  = $(this).data("w");
        var height  = $(this).data('h');
        var title = $(this).attr('title')? $(this).attr('title'):'信息';
		var maxfull= $(this).data("full");
        var index=layer.open({
            type: 2,
            title:title,
            area: [width+'px', height+'px'],
            fix: false, //不固定
            maxmin: true,
            content: [url,'no']
        });
		if(maxfull==1){
			layer.full(index);
		}
		
    });

	//冻结按钮layer弹出
	$('.layer_confirm').on('click',function(e){
		e.preventDefault();
		var title = $(this).attr('title');
		var url = $(this).prop('href');
		layer.confirm(title, {
		    btn: ['确认','再考虑一下'] //按钮
		}, function(){
			 $.getJSON(url).done(function(data) {
					if (data.state === 'success') {
						if (data.url) {
							
							location.href = data.url;
						} else {
							
							reloadPage(window);
						}
					} else if (data.state === 'fail') {
						layer.alert(data.info);
					}
				});
		});
	})
	//textarea字数限制
	$('.textarea').keyup(function(){
		var v =$(this).val();
		var l=v.length;
		var maxlength=$(this).data("length");
		if( l > maxlength){
			v = v.substring(0,maxlength);
			$(this).val(v);
			layer.alert("字数不能超过"+maxlength+"个字");
		}
		$(this).parent().find(".textarea-length").text(v.length);
	})
	$('.table tbody tr:odd').css("backgroundColor","#fafafa");
	$('.table tbody tr:even').hover(function(){
		$(this).css("backgroundColor","#fafafa")
	},function(){
		$(this).css("backgroundColor","#ffffff")
	})


      var ajaxForm_list = $('form.J_ajaxForm');
      if (ajaxForm_list.length) {
        if ($.support) {
              //ie8及以下，表单中只有一个可见的input:text时，会整个页面会跳转提交
              ajaxForm_list.on('submit', function (e) {
                  //表单中只有一个可见的input:text时，enter提交无效
                  e.preventDefault();
              });
        }

        $('.J_ajax_submit_btn').bind('click', function (e) {
            if (flag == false) {return false};
            e.preventDefault();
            var btn = $(this),
            form = btn.parents('form.J_ajaxForm');
            //ie处理placeholder提交问题
            if ($.support) {
                form.find('[placeholder]').each(function () {
                    var input = $(this);
                    if (input.val() == input.attr('placeholder')) {
                        input.val('');
                    }
                });
            }


            form.ajaxSubmit({
               url: btn.data('action') ? btn.data('action') : form.attr('action'),
                //按钮上是否自定义提交地址(多按钮情况)
                dataType: 'json',
                beforeSubmit: function (arr, $form, options) {
                    var text = btn.attr('value');

                    //按钮文案、状态修改
                    btn.attr('value',text + '中...').attr('disabled', true).addClass('disabled');
                },
                success: function (data, statusText, xhr, $form) {
                    var text =  btn.attr('value');
                    //按钮文案、状态修改
                    //btn.removeClass('disabled').attr('value','text.replace('中...', '')');
                    if (data.state === 'success') {
                       btn.attr('value',text.replace('中...', '') + '成功');
                        layer.msg('提交成功！', {
                          icon:1,
                          time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        },function(){

                          if (data.url) {
                                    //返回带跳转地址
                                    if($('.layer_show_window', window.parent.document).attr('href')){
                                        //iframe弹出页
                                        window.parent.location.href = data.url;
                                    }else{
                                        window.location.href = data.url;
                                    }
                                } else {
                                   
                                    if($('.layer_show_window', window.parent.document).attr('href')){
                                        reloadPage(window.parent);
                                    }else{
                                        //刷新当前页
                                        reloadPage(window);
                                    }
                                } 
                        });
                    } else if (data.state === 'fail') {
                        layer.msg(data.info,{shift: 6});
                        btn.removeProp('disabled').removeClass('disabled').attr('value',text.replace('中...', '') );
                        return false;
                    }
                }
            });
        });
      }

})